package com.gem.growingtree.activity;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.MenuItemCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;

import com.gem.growingtree.fragment.FragmentA;
import com.gem.growingtree.fragment.FragmentB;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

public class TodayPlayActivity extends FragmentActivity  implements ActionBar.TabListener{
	private FragmentA fragment1=new FragmentA();
	private FragmentB fragment2=new FragmentB();
	
	private static final int TAB_INDEX_COUNT=2;
	private static final int TAB_INDEX_ONE=0;
	private static final int TAB_INDEX_TWO=1;
	
	private ViewPager viewPager;
	private ViewPagerAdapter viewPagerAdapter;
	
	@ViewInject(R.id.TP_button_id)
	private Button button;
	@OnClick(R.id.TP_button_id)
	public  void onClickButton(){
		
	}
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		//requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.activity_today_play);  
		//getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.today_play_title);
		setUpActionBar();
		setUpViewPager();
		setUpTabs();
	}
	
	public boolean onCreateOptionsMenu(Menu menu) { 
        MenuItemCompat.setShowAsAction( 
                menu 
                .add(0, 1,1,"NO.1")
               .setTitle("任务记录"),
               // .setIcon(android.R.drawable.ic_menu_rotate),  
                MenuItemCompat.SHOW_AS_ACTION_IF_ROOM); 
        MenuItemCompat.setShowAsAction( 
                menu 
                .add(0, 2,2,"NO.2") 
                .setIcon(android.R.drawable.ic_menu_compass),  
                MenuItemCompat.SHOW_AS_ACTION_IF_ROOM); 
        /*MenuItemCompat.setShowAsAction( 
                menu 
                .add("No.3") 
                .setIcon(android.R.drawable.ic_menu_more),  
                MenuItemCompat.SHOW_AS_ACTION_IF_ROOM); */
        return true; 
    } 
	
	public boolean onOptionsItemSelected(MenuItem item){
	    switch (item.getItemId()) {
		case 2:
			Intent intent=new Intent();
			intent.setClass(TodayPlayActivity.this,PlayListActivity.class);
			startActivity(intent);
			break;
		 case android.R.id.home:
			Intent intent1=new Intent();
			intent1.setClass(TodayPlayActivity.this,PlayActivity.class);
			startActivity(intent1);
		  default:
			break;
		}	
	        return false;  
	    }
	  
		private void setUpActionBar() {
				// TODO Auto-generated method stub
				final ActionBar actionBar=getActionBar();
				//actionBar.setHomeButtonEnabled(false);
				actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
				actionBar.setDisplayShowTitleEnabled(false);
				actionBar.setDisplayShowHomeEnabled(true);
				actionBar.setDisplayHomeAsUpEnabled(true);
				//actionBar.addTab(actionBar.newTab().setText("sss").setTabListener(this));
			}
			private void setUpViewPager() {
				// TODO Auto-generated method stub
				viewPagerAdapter=new ViewPagerAdapter(getSupportFragmentManager());
				viewPager=(ViewPager) findViewById(R.id.pager);
				viewPager.setAdapter(viewPagerAdapter);
				viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
					public void onPageSelected(int position){
						final ActionBar actionBar=getActionBar();
						actionBar.setSelectedNavigationItem(position);	
			}
   public void onPageScrollStateChanged(int state){
				switch (state) {
				case ViewPager.SCROLL_STATE_IDLE:
					break;
				case ViewPager.SCROLL_STATE_DRAGGING:
					break;
				case ViewPager.SCROLL_STATE_SETTLING:
					break;
				default:
					break;
				}
			}
		});
	}
			private void setUpTabs() {
				// TODO Auto-generated method stub
				final ActionBar actionBar=getActionBar();
				for(int i=0;i<viewPagerAdapter.getCount();i++){
					actionBar.addTab(actionBar.newTab().setText(viewPagerAdapter.getPageTitle(i))
							.setTabListener(this));
				}
			}
	protected void onDestroy(){
		super.onDestroy();
	}
	
	public class ViewPagerAdapter extends FragmentPagerAdapter {

		public ViewPagerAdapter(FragmentManager fm) {
			super(fm);
			// TODO Auto-generated constructor stub
		}

		@Override
		public Fragment getItem(int position) {
			// TODO Auto-generated method stub
			switch (position) {
			case TAB_INDEX_ONE:
				return fragment1;
			case TAB_INDEX_TWO:
				return fragment2;
			}
		throw new IllegalStateException("NO fragment at currtent position"+position); 
		}
		public int getCount() {
			// TODO Auto-generated method stub
			return TAB_INDEX_COUNT;
		} 
		public CharSequence getPageTitle(int position){
			String tabLable=null;
			switch (position) {
			case TAB_INDEX_ONE:
				tabLable=getString(R.string.Write);
				break;
			case TAB_INDEX_TWO:
				tabLable=getString(R.string.Mode);
				break;
			}
			return tabLable;
		}
	}
	@Override
	public void onTabReselected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub
	}
	@Override
	public void onTabSelected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub
		viewPager.setCurrentItem(tab.getPosition());
	}
	@Override
	public void onTabUnselected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub
	 }
}
